const app = getApp();
import { submitReview } from '../../utils/requestApi.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrl: app.data.imgurl,
    rateValue: 3,
    list: null,
    listData: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (opt) {
    this.setData({
      list: JSON.parse(opt.list),
      listData: new Array(JSON.parse(opt.list).length).fill(JSON.stringify({ 
        ImageUrl1: [], 
        Score: 1, 
        ReviewText:'',
        ProductId: '',
        OrderId: opt.orderId,
        SkuId: '',
        SkuContent: '',
        SkuId: ''
      })).map(item => JSON.parse(item))
    })
    console.log(this.data.listData)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  //上传图片
  imgLoad(e) {
    let { index } = e.currentTarget.dataset;
    let { listData } = this.data;
    wx.chooseImage({
      count: 5,
      sourceType: ['album', 'camera'],
      success: (res) => {
        wx.showLoading({ title: '上传图片中...' })
        var uploadImgCount = 0;
        res.tempFilePaths.forEach(item =>{
          wx.uploadFile({
            url: app.data.url + '/API/ImageHandler.ashx?action=NewUpload',
            filePath: item,
            name: 'file',
            header: { Cookie: app.data.cookie },
            success: (res2) => {
              uploadImgCount++;
              if (uploadImgCount == res.tempFilePaths.length) wx.hideLoading();
              console.log(res2.data); 
              listData[index].ImageUrl1.push(JSON.parse(res2.data).Data.ImageURL)
              this.setData({ listData })
            }
          })
        })
      }
    })
  },

  //删除图片
  onClearImg(e){
    let { index, index2 } = e.currentTarget.dataset;
    let { listData } = this.data;
    listData[index].ImageUrl1.splice(index2, 1)
    this.setData({ listData })
  },

  //选择评分
  onRate(e){
    let { index } = e.currentTarget.dataset;
    let { listData } = this.data;
    listData[index].Score = e.detail;
    this.setData({ listData })
  },

  //输入评论内容
  onInput(e){
    let { index } = e.currentTarget.dataset;
    let { listData, list } = this.data;
    listData[index].ReviewText = e.detail.value;
    listData[index].SkuId = list[index].Id;
    listData[index].ProductId = list[index].ProductId;
    this.setData({ listData })
  },

  //提价
  onSubmit(){
    let { listData } = this.data;
    let isSubmit = true
    listData.forEach(item =>{
      if (!item.ReviewText) {
        wx.showToast({ icon: 'none', title: '请填写评论内容' })
        isSubmit = false
      }
      if (item.ImageUrl1.length > 1) item.ImageUrl1 = item.ImageUrl1.join(',')
      else if (item.ImageUrl1.length = 1) item.ImageUrl1 = item.ImageUrl1.join()
      else item.ImageUrl1 = ''
    })
    if (!isSubmit) return
    wx.showLoading({ title: '提交中...' })
    submitReview({ DataJson: JSON.stringify(listData) }).then(res => {
      wx.hideLoading();
      wx.showToast({ icon: 'none', title: res.data.Message })
      if (res.data.Status == 'Success') {
        setTimeout(res =>{
          wx.navigateBack({ delta: 1 })
        },1000)
      }
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})